package dk.twu.analyzer.model;

import dk.twu.analyzer.loader.ExchangeLoader;
import dk.twu.analyzer.util.AnalyzerConfig;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class ExchangeList implements IExchange {
    private final Map<String, Exchange> exchangeMap;

    public ExchangeList(Map<String, Exchange> allExchange) {
        exchangeMap = allExchange;
    }

    public static ExchangeList setupExchangeList(AnalyzerConfig analyzerConfig) {
        return new ExchangeList(ExchangeLoader.loadExchangeMap(analyzerConfig.getBaseDir(), analyzerConfig
                .getExchangeListFile()));
    }

    public Collection<Exchange> getAllExchange() {
        return exchangeMap.values();
    }

    @Override
    public Optional<Exchange> getExchange(String exchangeCode) {
        Exchange exchange = exchangeMap.get(exchangeCode);
        return exchange != null ? Optional.of(exchange) : Optional.empty();
    }

    public Map<String, Exchange> getExchangeMap() {
        return exchangeMap;
    }

    public Collection<Exchange> getExchanges(List<String> exchanges) {
        return exchangeMap.values().stream().filter(e -> exchanges.contains(e.getCode())).collect(Collectors.toList());
    }
}
